﻿using System;
using System.Collections.Generic;
using System.IO;

using Excel;

namespace BMCShowView.Business
{
    public static class ArrangeTable
    {
        public static List<List<string>> ReadFromStream(Stream stream)
        {
            // https://github.com/ExcelDataReader/ExcelDataReader
            var result = new List<List<string>>(31);

            using (var reader = ExcelReaderFactory.CreateOpenXmlReader(stream))
            {
                reader.IsFirstRowAsColumnNames = true;

                using (var dataset = reader.AsDataSet())
                {
                    using (var table = dataset.Tables["排班计算"])
                    {
                        for (var row = 0; row * 5 + 3 <= table.Rows.Count; ++row)
                        {
                            for (var day = 1 /* Monday */; day <= 7 /* Sunday */; ++day)
                            {
                                if (table.Rows[row * 5][day] != DBNull.Value) // 跳过月前空白
                                {
                                    var workOfDay = new List<string>(3);
                                    workOfDay.Add((string)table.Rows[row * 5 + 1][day]);
                                    if (table.Rows[row * 5 + 2][day] != DBNull.Value)
                                    {
                                        // 当天有三班
                                        workOfDay.Add((string)table.Rows[row * 5 + 2][day]);
                                    }
                                    workOfDay.Add((string)table.Rows[row * 5 + 3][day]);

                                    result.Add(workOfDay);
                                }
                            }
                        }
                    }
                }
            }

            return result;
        }
    }
}
